﻿using MessageHandlingServer.Server2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MessageHandlingServer.Server1
{
    class MyServer1 : IServer
    {
        IServer _server2;
        IQueue<IMessage> _queue = new MyBlockedQueue<IMessage>();

        public MyServer1(IServer server2)
        {
            _server2 = server2;
        }

        public bool ProcessMessage(IMessage message)
        {
            if(Validate(message))
            {
                new Thread(()=> _queue.Enqueue(message)).Start();
                new Thread(()=> _server2.ProcessMessage(_queue.Dequeue())).Start();
                return true;
            }
            else
            {
                return false;
            }
        }

        bool Validate(IMessage message)
        {
            try
            {
                message.ToText();
                message.GetValue();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
